<?php
namespace app\common\library;
use think\Cache;
use fast\Tool;
use think\config;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Share
 *
 * @author xpz
 */
class Wxtoken {
    private $appId;
    private $appSecret;
    private $cachewechatToken;
    private $cacheWechatTicket;
    
    public function __construct($appId,$appSecret){
        $this->appId = $appId;
        $this->appSecret = $appSecret;
        $this->cachewechatToken = 'cache_xpz_wechat_token21';
        $this->cacheWechatTicket = 'cache_xpz_wechat_ticket21';
    }
  
  public function accessToken() {
      if(Config::get('xpz.get_wx_token_by_interface') == 1){
          return $this->setAccessToken();
      } else {
          $accessTokenArr = $this->getAccessToken();
    //      print_r($accessTokenArr);exit;
          if(empty($accessTokenArr)){
              return $this->setAccessToken();
          } else {
              if($accessTokenArr['expire_time'] > time()){
    //              echo "sdf";exit;
                  Tool::mylog(Config::get('xpz.my_log_show'),'缓存获取Token：' , $accessTokenArr['access_token']."===".date("Y-m-d H:i:s",$accessTokenArr['expire_time']) , 'wxtoken');
                  return $accessTokenArr['access_token'];
              } else {
                  return $this->setAccessToken();
              }
          }
      }
      

  }
  
  private function setAccessToken(){
      // 如果是企业号用以下URL获取access_token
       //$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
      $res = json_decode($this->httpGet($url));
      if(isset($res->access_token) && !empty($res->access_token)){
          $arr['access_token'] = $res->access_token;
            $arr['expire_time'] = 7000+time();
            Cache::set($this->cachewechatToken,$arr,0);
            Tool::mylog(Config::get('xpz.my_log_show'),'接口获取Token：' , $arr['access_token'] ."===".date("Y-m-d H:i:s",$arr['expire_time']), 'wxtoken');
            return $arr['access_token'];
      } else {
          Tool::mylog(Config::get('xpz.my_log_show'),'接口获取Token：' , '失败！' , 'wxtoken');
      }
        

  }
  
  private function getAccessToken(){
      return Cache::get($this->cachewechatToken);
  }

  private function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
//    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
  }

}
